package com.netty.client;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSON;
import com.netty.listener.DefaultGenericFutureListener;
import com.netty.pool.NettyClientPoolFactory;
import com.netty.util.MessageBody;
import com.netty.util.MessageHeader;
import com.netty.util.NettyMessage;
import com.netty.util.WrapperRequestMessage;
import com.netty.util.WrapperResponseMessage;

import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.pool.ChannelPool;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.concurrent.Future;

public class ClientTest {

	public static void main(String[] args) {
		testHttp();
	}
	
	public static void testHttp(){
		NettyClientPoolFactory factory = new NettyClientPoolFactory();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
		ChannelPool pool = factory.getPool("locahost");
		Future<Channel> future= pool.acquire();
		String messageBody = build();
		WrapperRequestMessage request = new WrapperRequestMessage();
		request.setBody(postRequestBody(messageBody));
		DefaultGenericFutureListener listener = NettyClient.sendMessage(future, request);
		try {
			WrapperResponseMessage responseMessage = listener.get(15, TimeUnit.MILLISECONDS);
			System.out.println(responseMessage.getBody());
		}finally{
			try {
				pool.release(future.get());
			} catch (InterruptedException | ExecutionException e) {
				e.printStackTrace();
			}
		}
	}
	

	 public static Object getRequestBody(){
		URI uri = null;
		try {
			uri = new URI("127.0.0.1");
		} catch (URISyntaxException e) {
			
		}
		DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.toASCIIString());
        request.headers().set(HttpHeaderNames.HOST, "127.0.0.1:8080");
        request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
        request.headers().set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.GZIP);
	 	return request;
	}
	 
	 public static Object postRequestBody(String content){
		    URI uri = null;
			try {
				uri = new URI("127.0.0.1");
			} catch (URISyntaxException e) {
				
			}
			DefaultFullHttpRequest request = null;
			try {
				request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uri.toASCIIString(), Unpooled.wrappedBuffer(content.getBytes("UTF-8")));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
	        request.headers().set(HttpHeaderNames.HOST, "127.0.0.1:8080");
	        request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
	        request.headers().set(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes());
	        request.headers().set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.GZIP);
		 	return request; 
	 }
	 
	 public static String build(){
		 	NettyMessage message = new NettyMessage();
			MessageHeader header = new MessageHeader();
			MessageBody body = new MessageBody();
			message.setHeader(header);
			message.setBody(body);
			
			body.setIp("127.0.0.1");
			body.setAge(23);
			body.setName("小张");
			
			header.setCrcCode("chenmutime");
			header.setSessionID(124141242342L);
			header.setType(0);
			String messageBody = JSON.toJSONString(message);
			
			return messageBody;
	 }
}
